// pages/shouye/makeorder/makeorder.js
const app = getApp();
var url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['到店取餐', '送餐上门'],
    currentTab: 0,
    list:{},
    Mealsfee:2,
    totalPrice: 0, 
    totalPrices:0,
    index:0,
    item:[],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var list = JSON.parse(options.list);
    console.log(list)
    that.setData({
      list: list
    });
    this.Shipping();
    this.getTotalPrice();
  },
  onShow:function(){
    this.Shipping();
  },
  //到店取餐的确认订单
  pay: function (e) {
    var that = this;
    let list = this.data.list;
    const userid = app.globalData.userid;
    let arr = [];
    var typeshop = 1;
    for (let i = 0; i < list.length; i++) {
      if (list[i].checked) {
        arr.push(list[i]);
      }
    }
    that.setData({
      item: arr,
    });
    var lists = this.data.item;
    var listss = JSON.stringify(lists);
    var totalPrice = this.data.totalPrice;
    // var Mealsfee = this.data.Mealsfee;
    wx.request({
      url: url + 'submitOrderMultiGoods',
      data: {
        userId: userid,
        orderType: 1,
        orderGoodsForm: listss,
        goodsAmount: totalPrice,
        payId:3,
        packFee: 2,
        payName: '在线支付',
        mName: '楚阳一食堂',
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        var orderId = res.data.object;
        var price = that.data.totalPrice;
        wx.navigateTo({
          url: '../paymentorder/paymentorder?orderId=' + orderId + '&price=' + price+'&typeshop='+ typeshop ,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
        })
      }
    })
   
  },
  pay1: function (e) {
    var that = this;
    let list = this.data.list;
    const userid = app.globalData.userid;
    let arr = [];
    var typeshop =2;
    for (let i = 0; i < list.length; i++) {
      if (list[i].checked) {
        arr.push(list[i]);
      }
    }
    that.setData({
      item: arr,
    });
    var lists = this.data.item;
    var listss = JSON.stringify(lists);
    var totalPrices = this.data.totalPrices;
    // var Mealsfee = this.data.Mealsfee;
    var consignee = e.currentTarget.dataset.name;
    var phone = e.currentTarget.dataset.phone;
    var address = e.currentTarget.dataset.address;

    wx.request({
      url: url+'submitOrderMultiGoods',
      data: {
        userId: userid,
        orderType: 1,
        orderGoodsForm: listss,
        goodsAmount: totalPrices,
        shippingFee:3,
        packFee: 2,
        payName:'在线支付',
        mName:'楚阳一食堂',
        mobile: phone,
        address:address,
        consignee: consignee,
        payId: 3,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        var orderId = res.data.object;
        var price = that.data.totalPrices;
               
          wx.navigateTo({
            url: '../paymentorder/paymentorder?orderId='+orderId+'&price='+price+'&typeshop='+typeshop,
          success: function (res) { },
          fail: function (res) { },
          complete: function (res) { },
          })
        }
    })
  },
  navbarTap: function (e) {
    this.setData({
      currentTab: e.currentTarget.dataset.idx
    })
  }, 
  //默认地址展示
  Shipping: function () {
    var that = this;
    var index = this.data.index;
    const userid = app.globalData.userid
    wx.request({
      url: app.globalData.url + 'getMemberAddressByUserId',
      data: {
        userId: userid
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        if (res.data.object[index] == undefined) {
          console.log("请添加收货地址")
        } else {
          that.setData({
            userAddress: res.data.object[index],
          });
          that.data.orderName = res.data.object[index].consignee;
          that.data.mobile = res.data.object[index].mobile;
          that.data.address = res.data.object[index].address
        }
      }
    });
  },
  //选择地址
  address:function(){
    const userid = app.globalData.userid;
    const typeShopping = 1
    wx.navigateTo({
      url: '../../mine/address/address?userid=' + userid + '&typeShopping=' + typeShopping,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  
  /**
 * 计算总价
 */
  getTotalPrice() {
    var that = this;
    var list = this.data.list;                 // 获取购物车列表
    var total = 0;
    var fee = 0;
    for (var i = 0; i < list.length; i++) {       
       // 循环列表得到每个数据
      total += list[i].goodsNumber * list[i].goodsPrice;
    }
    
    fee += list.length * 2;
    that.setData({                               // 最后赋值到data中渲染到页面
      list: list,
      Mealsfee: fee,
      totalPrice: total + fee,
      totalPrices: total + fee 
    });
  },
})